package org.todd.healthreport.service.Impl;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.Properties;

public class BaseChromeDriver {


    @Value("${selenium.driver}")
    private String seleniumDriver;

    @Value("${selenium.linux.driver.file}")
    private String seleniumLinuxDriverFile;

    @Value("${selenium.window.driver.file}")
    private String seleniumWindowDriverFile;

    private ChromeDriver getLinuxChromeDriver(){
        System.setProperty(seleniumDriver,seleniumLinuxDriverFile);
        ChromeOptions options = new ChromeOptions();
        options.setHeadless(Boolean.TRUE);
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");
        return new ChromeDriver(options);
    }

    private ChromeDriver getWindowsChromeDriver(){
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("classpath:"+seleniumWindowDriverFile);
        try {
            System.setProperty(seleniumDriver,resource.getURL().getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ChromeDriver();
    }


    ChromeDriver getChromeDriver(){
        Properties props = System.getProperties();
        String sysName = props.getProperty("os.name");
        switch (sysName){
            case "Windows 10":
                return getWindowsChromeDriver();
            case "Linux":
                return getLinuxChromeDriver();
            default:
                return  null;
        }
    }

}
